package org.linlinjava.litemall.wx.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallOrder;
import org.linlinjava.litemall.db.dto.CloverChargesDTO;
import org.linlinjava.litemall.db.service.LitemallOrderService;
import org.linlinjava.litemall.wx.annotation.LogAnno;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.WxCloverPayService;
import org.linlinjava.litemall.wx.util.RewardsUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
/**
 * 三叶草支付控制器
 *
 * @author yliyun
 * @date 2022/09/14
 */
@Api(tags = "Clover支付API")
@Slf4j
@RestController
@RequestMapping("/wx/cloverPay")
@RequiredArgsConstructor
public class CloverPayController {
    private final WxCloverPayService wxCloverPayService;
    private final LitemallOrderService orderService;
    private final RewardsUtils rewardsUtils;

    @ApiOperation("Clover支付-「购买商品|支付」")
    @GetMapping("pay/success")
    @LogAnno
    @Transactional(rollbackFor = Exception.class)
    public ResponseUtil.Response success(@LoginUser Integer userId,
                                         @NotNull @RequestParam("orderId") Integer orderId,
                                         @RequestParam("token") String token){
        Object obj =  wxCloverPayService.executePayment(token,userId, orderId);
        if(!(obj instanceof CloverChargesDTO)){
            return ResponseUtil.fail(obj);
        }
        CloverChargesDTO cloverChargesDTO = (CloverChargesDTO)obj;
        //是否第一次消费
        rewardsUtils.getRewardInConsum(userId);
        LitemallOrder order = orderService.findByTransactionId(cloverChargesDTO.getId());
        wxCloverPayService.presentPoint(order.getId());
        //用户 等级会员的提升
        wxCloverPayService.upgradeUserLevel(order.getId());
        return ResponseUtil.ok("success");
    }

    @ApiOperation("Clover支付-「充值余额|支付」")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "amount",value = "金额"),
            @ApiImplicitParam(name = "token",value = "Cvl令牌")
    })
    @GetMapping("recharge/balance")
    @LogAnno
    public ResponseUtil.Response rechargeBalance(@ApiIgnore @LoginUser Integer userId,
                                                 @NotNull @RequestParam("amount")BigDecimal amount,
                                                 @RequestParam("token")String token){
        return wxCloverPayService.rechargeBalance(userId,amount,token);
    }

    @ApiOperation("Clover-「退款」")
    @GetMapping("refund")
    @LogAnno
    public Object refund( @NotNull @RequestParam("orderId") Integer orderId) throws Exception{
        return wxCloverPayService.refund(orderId);
    }
}
